<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<my-search @handleclick="gotoSearch"></my-search>
		</view>
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in swiperList" :key="item.goods_id">
				<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
					<image :src="item.image_src" mode=""></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 导航栏 -->
		<view class="nav_list">
			<view class="nav_item" v-for="(item,index) in navList" :key="index" @click="navClickHandler(item)">
				<image :src=" item.image_src" mode="">
				</image>
			</view>
		</view>
		<!-- 楼层 -->
		<view class="floor_list">
			<view class="floor_item" v-for="(item,index) in floorList" :key="index">
				<image class="floor_title" :src="item.floor_title.image_src" mode=""></image>
				<view class="floor_img">
					<view class="left_img">
						<navigator :url="item.product_list[0].url">
							<image :src="item.product_list[0].image_src"
								:style="{width:item.product_list[0].image_width+'rpx'}" mode="widthFix"></image>
						</navigator>
					</view>
					<view class="right_img">
						<navigator v-for="(v,i) in item.product_list" :key="i" v-if="i" :url="v.url">
							<image :src="v.image_src" mode="widthFix" :style="{width: v.image_width+'rpx'}"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// 导入自己封装的 mixin 模块
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		// 将 badgeMix 混入到当前的页面中进行使用
		mixins: [badgeMix],
		data() {
			return {
				swiperList: [], // 轮播图数据
				navList: [], // 导航数据
				floorList: [] // 楼层数据
			};
		},
		onLoad() {
			// 在小程序刚加载的时候 执行的钩子
			this.getSwiperList()
			this.getNavList()
			this.getfloorList()
		},
		methods: {
			// 调用轮播图接口
			async getSwiperList() {
				const {
					data
				} = await uni.$http.get('/home/swiperdata')
				// console.log(data)
				if (data.meta.status !== 200) return uni.$showMsg()
				this.swiperList = data.message
			},
			// 调用导航栏接口
			async getNavList() {
				const {
					data
				} = await uni.$http.get('/home/catitems')
				// console.log(data)
				if (data.meta.status !== 200) return uni.$showMsg()
				this.navList = data.message
			},
			// 调用楼层接口
			async getfloorList() {
				const {
					data: res
				} = await uni.$http.get('/home/floordata')
				// console.log(res)
				if (res.meta.status !== 200) return uni.$showMsg()
				res.message.forEach(item => {
					item.product_list.forEach(v => {
						v.url = '/subpkg/goods_list/goods_list?' + v.navigator_url.split('?')[1]
					})
				})
				this.floorList = res.message
			},
			// 点击每个商品分类进入对应的商品列表
			navClickHandler(item) {
				if (item.name === '分类') {
					// 下面这个方法是用来做跳转的
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			// 点击搜索
			gotoSearch() {
				// console.log('点击了搜索框')
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		width: 750rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%
		}
	}

	.nav_list {
		display: flex;
		justify-content: space-around;
		margin: 32rpx 0;

		image {
			width: 128rpx;
			height: 140rpx;
		}

	}

	.floor_title {
		height: 60rpx;
		width: 100%;
	}

	.floor_img {
		display: flex;
		padding-left: 10rpx;

		.right_img {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
		}
	}

	// 吸顶搜索框
	.search-box {
		// 设置定位效果为“吸顶”
		position: sticky;
		// 吸顶的“位置”
		top: 0;
		// 提高层级，防止被轮播图覆盖
		z-index: 9;
	}
</style>
